﻿using System;
using AutoMapper;
using Demo.Entities;
using Demo.Models.Dto;

namespace Demo.Profiles
{
    public class EmployeeProfiles : Profile
    {
        public EmployeeProfiles()
        {
            CreateMap<Employee, EmployeeDto>()
                .ForMember(
                    dest => dest.Name,
                    opt => opt.MapFrom(o => o.LastName + o.FirstName))
                .ForMember(dest => dest.Age,
                    opt => opt.MapFrom(o => DateTime.Now.Year - o.DateOfBirth.Year))
                .ForMember(dest => dest.Gender,
                    opt => opt.MapFrom(o => o.Gender.ToString()));

            CreateMap<EmployeeAddDto, Employee>()
                .ForMember(
                    dest => dest.Gender,
                    opt => opt.MapFrom(o => Enum.Parse<Gender>(o.Gender)));

            CreateMap<EmployeeUpdateDto, Employee>()
                .ForMember(
                    dest => dest.Gender,
                    opt => opt.MapFrom(o => Enum.Parse<Gender>(o.Gender)));

            CreateMap<EmployeeUpdateDto, EmployeeAddDto>();

            CreateMap<Employee, EmployeeUpdateDto>()
                .ForMember(dest => dest.Gender,
                    opt =>
                        opt.MapFrom(o => o.Gender.ToString()));
        }
    }
}